package com.yantu.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yantu.bean.Subject;
import com.yantu.bean.TaskExam;
import com.yantu.bean.TextContent;
import com.yantu.bean.UserEventLog;
import com.yantu.mapper.TaskExamMapper;
import com.yantu.mapper.TextContentMapper;
import com.yantu.mapper.UserEventLogMapper;
import com.yantu.util.RedisUtils;
import com.yantu.util.Result;
import com.yantu.view.admin.AdminPageView;
import com.yantu.view.admin.ItemsView;
import com.yantu.view.admin.TaskView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 综合实训课程设计
 * @since 2022-08-14
 */
@RestController
@RequestMapping("/admin")
@Api("管理端任务接口")
public class AdminTaskExamController extends AdminUserEventLogController{
    @Autowired
    TaskExamMapper taskExamMapper;

    @Autowired
    TextContentMapper textContentMapper;

    @Autowired
    RedisUtils redisUtils;

    //任务列表
    @GetMapping("/taskExamList")
    @ApiOperation("任务列表")
    public Result myListsSubject(){
        if(redisUtils.haskeysf("adminTaskExamList")){
            System.out.println("缓存里面有");
            List<TaskExam> taskExamList = (List<TaskExam>) redisUtils.getValue("adminTaskExamList");
            return Result.succ(200, "查询成功", taskExamList);
        }else {
            List<TaskExam> taskExamList = taskExamMapper.selectList(null);
            redisUtils.saveValue("adminTaskExamList",taskExamList);
            return Result.succ(200, "查询成功", taskExamList);
        }
    }
    //任务分页
    @GetMapping("/taskExams ")
    @ApiOperation("任务分页信息")
    public Result myListTaskExam(@ModelAttribute AdminPageView pageView) {
        PageHelper.startPage(pageView.getPageIndex(), pageView.getPageSize());
        if(redisUtils.haskeysf("adminTaskExamList"+pageView)){
            System.out.println("缓存里面有");
            PageInfo pageInfo = (PageInfo) redisUtils.getValue("adminTaskExamList"+pageView);
            return Result.succ(200, "查询成功", pageInfo);
        }else {
            QueryWrapper<TaskExam> queryWrapper = new QueryWrapper<>();
            if (pageView.getGradeLevel() != null) {
                queryWrapper.eq("grade_level", pageView.getGradeLevel());
            }
            List<TaskExam> taskExamList = taskExamMapper.selectList(queryWrapper);
            PageInfo pageInfo = new PageInfo(taskExamList);
            redisUtils.saveValue("adminTaskExamList"+pageView,pageInfo);
            return Result.succ(200, "查询成功", pageInfo);
        }
    }

    //任务查询
    @GetMapping("/taskExam/{id}")
    @ApiOperation("根据任务表id查询任务表")
    public Result getTaskExamById(@PathVariable("id") Integer id) {
        if(redisUtils.haskeysf("adminTaskExamList"+id)){
            System.out.println("缓存里面有");
           TaskExam taskExam= (TaskExam) redisUtils.getValue("adminTaskExamList"+id);
            return Result.succ(200, "查询成功", taskExam);
        }else {
            System.out.println(id);
            TaskExam taskExam = taskExamMapper.selectById(id);
            System.out.println(taskExam);
            if (taskExam != null) {
                redisUtils.saveValue("adminTaskExamList"+id,taskExam);
                return Result.succ(200, "查询成功", taskExam);
            } else {
                return Result.fail("没有该任务");
            }
        }
    }

    //任务删除
    @DeleteMapping("/taskExam/{id}")
    @ApiOperation("删除任务")
    public Result deleteUser(@PathVariable("id") Integer id) {

        int result = taskExamMapper.deleteById(id);
        if(result>0){
            super.adminLog("admin删除了任务"+id);
            redisUtils.delAll();
            return Result.succ("删除成功",null);
        }else {
            return Result.fail("删除失败");
        }
    }

    //任务创编
    @PostMapping("/taskExam")
    @ApiOperation("添加任务")
    public Result addTask(@RequestBody TaskView taskView){
        System.out.println("到这里了");
        TextContent textContent = new TextContent();
        TaskExam taskExam = new TaskExam();
        //设置任务标题
        taskExam.setTitle(taskView.getTitle());
        taskExam.setCreateUser(2);
        taskExam.setCreateUserName("admin");
        taskExam.setCreateTime(new Date());
        System.out.println(taskExam);
        //设置任务年级
        taskExam.setGradeLevel(taskView.getGradeLevel());
        System.out.println("开始设置任务试卷");
        List<ItemsView> taskPaperList = taskView.getPaperItems();
        System.out.println("设置content");
        //list转换未json字串
        JSONArray jsonarray = JSONArray.fromObject(taskPaperList);
        System.out.println(jsonarray);
        textContent.setContent(jsonarray.toString());
        //向content表插入任务试卷列表
        int result = textContentMapper.insert(textContent);
        if (result > 0) {
            //获取任务试卷内容contentID
            taskExam.setFrameTextContentId(textContent.getId());
            //添加任务
            int result1 = taskExamMapper.insert(taskExam);
            if (result1 > 0) {
                System.out.println(taskExam);
                super.adminLog("admin发布了任务:"+taskExam.getTitle());
                redisUtils.delAll();
                return Result.succ(200, "添加成功", taskExam);
            } else {
                textContentMapper.deleteById(textContent.getId());
                return Result.fail("添加失败", null);
            }
        } else {
            return Result.fail("添加失败", null);
        }
    }

    //任务编辑(测试成功)
    @PutMapping("/taskExam")
    @ApiOperation("编辑任务信息")
    public Result editUserMessage(@ModelAttribute TaskView taskView)  {
        UpdateWrapper updateQuery = new UpdateWrapper<>();
        updateQuery.eq("id",taskView.getId());
        if(taskView.getGradeLevel()!=null){
            updateQuery.set("grade_level",taskView.getGradeLevel());
        }
        if(taskView.getTitle()!=null){
            updateQuery.set("title",taskView.getTitle());}
        //获得并存储任务的试卷信息
        List<ItemsView> taskPaperList=taskView.getPaperItems();
        //list转换未json字串
        JSONArray jsonarray = JSONArray.fromObject(taskPaperList);
        //content表修改任务试卷列表
        System.out.println("任务ID"+taskView.getId());
        TaskExam taskExam=taskExamMapper.selectById(taskView.getId());
        System.out.println("找到这个任务了"+taskExam);
        System.out.println("找到任务contentId了"+taskExam.getFrameTextContentId());

        TextContent textContent=textContentMapper.selectById(taskExam.getFrameTextContentId());
        System.out.println("找到任务content内容了");

        System.out.println("开始更新");
        UpdateWrapper updateQuery1 = new UpdateWrapper<>();
        updateQuery1.eq("id",taskExam.getFrameTextContentId());
        if(jsonarray!=null){
            updateQuery1.set("content",jsonarray.toString());
            updateQuery1.set("create_time",new Date());
        }
        //更新content
        System.out.println("更新的content内容"+updateQuery1.toString());
        int result=textContentMapper.update(null,updateQuery1);
        if(result>0){
            //更新任务
            System.out.println("更新content成功，更新任务表");
            int result1  = taskExamMapper.update(null,updateQuery);
            if(result1>0){
                //添加日志
                super.adminLog("admin成功修改了任务"+taskView.getTitle());
                redisUtils.delAll();
                return Result.succ(200,"编辑成功",null);
            }else {
                return Result.fail("编辑失败");
            }
        }else{
            return Result.fail("编辑失败",null);
        }
    }

}

